package pers.zw.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * 获得客户机请求头
 */
@WebServlet(name = "ServletRequestDemo2", urlPatterns = "/ServletRequestDemo2")
public class ServletRequestDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("content-type", "text/html; charset = UTF-8");
        PrintWriter out = response.getWriter();
        Enumeration<String> reqHeadInfos = request.getHeaderNames();
        out.write("客户端请求头信息：<hr/>");

        while (reqHeadInfos.hasMoreElements()) {
            String headName = reqHeadInfos.nextElement();
            String headValue = request.getHeader(headName);

            out.write(headName + ": " + headValue + "<br/>");
        }

        out.write("<br/>客户端Accept-Encoding请求头的值：<hr/>");
        String value = request.getHeader("Accept-Encoding");
        out.write(value);

        Enumeration<String> e = request.getHeaders("Accept-Encoding");
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
    }
}
